﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using SysFrame.BP;
using SysFrame.Message;

namespace AppServer.Controllers.System
{
    [Route("api/[controller]")]
    [ApiController]
    public class System10Controller : SysControllerBase
    {
        //功能
        // POST: api/System6
        [HttpPost]
        public ActionResult<string> Post([FromBody] dynamic data)
        {
            string jsTimeStamp = data.jsTimeStamp;
            string Action = data.Action == null ? "Get" : data.Action;
            string rtstr = "";
            //得到列表
            if (Action == "Get")
            {
                rtstr = Get(data);
            }
            //得到列表
            if (Action == "GetSystem1")
            {
                rtstr = GetSystem1(data);
            }
            //得到列表
            if (Action == "GetSystem12")
            {
                rtstr = GetSystem12(data);
            }
            //得到列表
            if (Action == "GetSystem13")
            {
                rtstr = GetSystem13(data);
            }
            if (Action == "Save")
            {
                rtstr = Save(data);
            }
            //删除用户
            if (Action == "RemoveUser")
            {
                rtstr = RemoveUser(data);
            }
            //新增用户
            if (Action == "addUser")
            {
                rtstr = addUser(data);
            }
            //选择的菜单
            if (Action == "SaveSys12")
            {
                rtstr = SaveSys12(data);
            }
            //选择的按钮
            if (Action == "SaveSys13")
            {
                rtstr = SaveSys13(data);
            }
            return rtstr;
        }

        /// <summary>
        /// 得到角色列表
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Get(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string search = postdata["search"].ToString();
            BLLsystem_10 bll = new BLLsystem_10();
            List<Mssystem_10> mssys10 = new List<Mssystem_10>();
            Mssystem_1 mssys1 = new Mssystem_1();
            MsQuery msq = new MsQuery();
            if (!string.IsNullOrEmpty(search))
            {
                msq.strquery += " and (SYSTEM_10_20 like '%" + search + "%' )";
            }
            mssys10 = bll.Getsystem_10MsByQuery(msq);
            return JArray.FromObject(mssys10).ToString();
        }

        /// <summary>
        /// 得到角色用户列表
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string GetSystem1(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string str10 = postdata["str10"].ToString();
            MsQuery msq = new MsQuery();
            msq.strquery += " and 1=1 and SYSTEM_R3_30 ='" + str10 + "' and SYSTEM_R3_40='1'";
            List<Mssystem_r3> listms = new BLLsystem_r3().Getsystem_r3MsByQuery(msq);
            msq = new MsQuery();
            //条件
            if (true)
            {
                msq.strquery += " and 1=1 and SYSTEM_1_10 in (" + string.Join(",", listms.ConvertAll(m => m.System_R3_20)) + ") ";
                //ms.strorder = " SYSTEM_7_100 ";
            }
            //DataTable dt = ((DataSet)new BLLsystem_1().Getsystem_1ByQuery(msq).data).Tables[0];
            List<Mssystem_1> listms1 = new List<Mssystem_1>();
            try
            {
                listms1 = new BLLsystem_1().Getsystem_1MsByQuery(msq);
            }
            catch (Exception)
            {

                throw;
            }
            return JArray.FromObject(listms1).ToString();
        }

        /// <summary>
        /// 得到角色菜单列表
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string GetSystem12(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string str10 = postdata["str10"].ToString();
            List<Mssystem_12> listms12 = new List<Mssystem_12>();
            MsQuery msq12 = new MsQuery();
            msq12.strquery += " and  System_12_20 = '" + str10 + "' ";
            msq12.strquery += " and  SYSTEM_12_40 = '1' ";
            listms12 = new BLLsystem_12().Getsystem_12MsByQuery(msq12);
            return JArray.FromObject(listms12).ToString();
        }

        /// <summary>
        /// 得到角色菜单列表
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string GetSystem13(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            //string str30 = postdata["str30"].ToString();
            string str20 = postdata["str20"].ToString();
            List<Mssystem_13> listms13 = new List<Mssystem_13>();
            MsQuery msq13 = new MsQuery();
            msq13.strquery += " and  System_13_20 = '" + str20 + "' ";
            //msq13.strquery += " and  System_13_30 = '" + str30 + "' ";
            msq13.strquery += " and  SYSTEM_13_70 = '1' ";
            listms13 = new BLLsystem_13().Getsystem_13MsByQuery(msq13);
            return JArray.FromObject(listms13).ToString();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Save(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            //Mssystem_10 ms = Mssystem_10.parse<Mssystem_10>(postdata);
            Mssystem_10 ms = new Mssystem_10();
            ms.System_10_60 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            MsReturned rt = new BLLsystem_10().Savesystem_10(ms);
            return JObject.FromObject(rt).ToString();
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string RemoveUser(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string str20 = postdata["str20"].ToString();
            string str30 = postdata["str30"].ToString();
            MsQuery msq = new MsQuery();
            msq.strquery += " and System_R3_30 = " + str30 + " and System_R3_20 in ('" + str20 + "')";
            MsReturned rt = new BLLsystem_r3().Deletesystem_r3ByQuery(msq);
            return JObject.FromObject(rt).ToString();
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string addUser(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string str20 = postdata["str20"].ToString();
            string str30 = postdata["str30"].ToString();
            Mssystem_r3 r3 = new Mssystem_r3();
            MsReturned rt = new MsReturned();

            r3.System_R3_30 = str30;
            r3.System_R3_40 = "1";
            r3.System_R3_50 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            try
            {
                foreach (string item in str20.Split(","))
                {
                    r3.System_R3_20 = item;
                    rt = new BLLsystem_r3().Savesystem_r3(r3);
                }
            }
            catch { }
            return JObject.FromObject(rt).ToString();
        }
        /// <summary>
        /// 选择的菜单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string SaveSys12(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string str20 = postdata["str20"].ToString();
            string str30 = postdata["str30"].ToString();
            string type = postdata["type"].ToString();

            MsReturned rt = new MsReturned();
            if (type == "0")
            {
                MsQuery msq12 = new MsQuery();
                msq12.strquery += " and SYSTEM_12_20='" + str20 + "'";
                msq12.strquery += " and SYSTEM_12_30='" + Convert.ToInt32(str30) + "'";
                rt = new BLLsystem_12().Deletesystem_12ByQuery(msq12);
            }
            else
            {
                Mssystem_12 ms12 = new Mssystem_12();
                ms12.System_12_20 = str20;
                ms12.System_12_30 = Convert.ToInt32(str30);
                ms12.System_12_40 = "1";
                rt = new BLLsystem_12().Savesystem_12(ms12);
            }
            return JObject.FromObject(rt).ToString();
        }
        /// <summary>
        /// 选择的按钮
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string SaveSys13(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string str20 = postdata["str20"].ToString();
            string str30 = postdata["str30"].ToString();
            string str40 = postdata["str40"].ToString();
            string str50 = postdata["str50"].ToString();
            string type = postdata["type"].ToString();

            MsReturned rt = new MsReturned();
            if (type == "0")
            {
                MsQuery msq = new MsQuery();
                msq.strquery = " and System_13_20='" + str20 + "'";
                msq.strquery += " and System_13_30='" + str30 + "'";
                //msq.strquery += " and System_13_40='" + str40 + "'";
                msq.strquery += " and System_13_50='" + str50 + "'";
                rt = new BLLsystem_13().Deletesystem_13ByQuery(msq);
            }
            else
            {
                Mssystem_13 ms13 = new Mssystem_13();
                ms13.System_13_20 = str20;
                ms13.System_13_30 = str30;
                ms13.System_13_40 = str40;
                ms13.System_13_50 = str50;
                ms13.System_13_70 = "1";
                rt = new BLLsystem_13().Savesystem_13(ms13);
            }
            return JObject.FromObject(rt).ToString();
        }
    }
}